<?php
include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */
if ($_GET['id_ad'] == "0")
{
    if ($_POST['click'] == "1")
    {
        //$sql_q = '" INSERT INTO `field` (`id`,`name`,`divname`,`parent_id`) VALUES (NULL,\''.$_POST['name'].'\',\''.$_POST['divname'].'\','.intval($_POST['parent_id']).') "';
        $cc1 = intval($_POST['cc']);
        $i=0;
        /*if ($_POST['parent_id'] <> "")
        {
        $sql_q = 'DELETE FROM `field_matrix` WHERE `parent_id`='.intval($_POST['parent_id']);
        $result = mysqli_query($con, $sql_q);
        }*/
        while ($cc1>$i)
        {
        $i=$i+1;
        $ffield_id = 'field_id'.$i;
        $iid = 'fffid'.$i;
        $oordering = 'ordering'.$i;
            $sql_qq = 'SELECT * FROM field_matrix WHERE field_id='.intval($_POST[$ffield_id]).' LIMIT 1';
            $result_qq = $result = mysqli_query($con, $sql_qq);
            $row_qq = mysqli_fetch_array($result_qq);
            $found = 0;
            if (intval($_POST[$ffield_id]) != "")
            {
                // echo $sql_qq.'<br>';
                if (empty($row_qq)){
                    $found_f =0;
                }
                else
                {
                    $found_f = 1;
                }
                if ($found_f == 1)
                {
                    //    echo $oordering.'='.$_POST[$oordering].'<br>';
                    $sql_q = 'UPDATE field_matrix SET field_id='.intval($_POST[$ffield_id]).',parent_id='.intval($_POST['parent_id']).',ordering='.intval($_POST[$oordering]).' WHERE id='.intval($row_qq['id']);
                    $result = mysqli_query($con, $sql_q);
                    //echo $sql_q.'<br>';
                }
                else
                {
                    $sql_q = 'INSERT INTO field_matrix (`id`,`field_id`,`parent_id`,`ordering`) VALUES (NULL,'.intval($_POST[$ffield_id]).','.intval($_POST['parent_id']).','.intval($_POST[$oordering]).')';
                    //echo $sql_q.'<br>';
                    $result = mysqli_query($con, $sql_q);
                }
            }
            else
            {
                $sql_q = 'DELETE FROM field_matrix WHERE parent_id='.intval($_POST['parent_id']).' AND field_id='.intval($_POST[$iid]);
                // echo $sql_q.'<br>';
                $result = mysqli_query($con, $sql_q);
            }
        /*if ($_POST[$ffield_id]<>"")
        {
        /*if ($_POST[$iid]=="")
        {*/
        //$sql_q = 'UPDATE `field_matrix` SET `id`=[value-1],`field_id`='.intval($_POST[$ffield_id]).',`parent_id`='.intval($_POST['parent_id']).',`ordering`='.intval($_POST[$oordering]).' WHERE 1';
        /*if ($_POST['$iid'] == "")
        {
            $_iid = 'NULL';
        }
        else
        {
            $_iid = intval($_POST['$iid']);
        }
        $sql_q = 'INSERT INTO field_matrix (`id`,`field_id`,`parent_id`,`ordering`) VALUES ('.$_iid.','.intval($_POST[$ffield_id]).','.intval($_POST['parent_id']).','.intval($_POST[$oordering]).')';
        $result = mysqli_query($con, $sql_q);
//        echo $sql_q.'<br>';
        /*}
        else
        {
        $sql_q = 'UPDATE `field_matrix` SET `id`='.intval($_POST[$iid]).',`field_id`='.intval($_POST[$ffield_id]).',`parent_id`='.intval($_POST['parent_id']).',`ordering`='.intval($_POST[$oordering]).' WHERE id='.intval($_POST[$iid]);
        //$sql_q = 'INSERT INTO field_matrix (`id`,`field_id`,`parent_id`,`ordering`) VALUES (NULL,'.intval($_POST[$ffield_id]).','.intval($_POST['parent_id']).','.intval($_POST[$oordering]).')';
        $result = mysqli_query($con, $sql_q);
        echo $sql_q.'<br>';
        }*/
        }

    }

    $click = "0";
    $_POST["click"] = "0";
    //if (!ISSET($_POST['parent_id']))
    //{
    //    $_POST['parent_id'] = 0;
   // }
?>
    <script>
        function my_yoz(){
            document.form_field_matrix_insert.click.value='0';
            document.getElementById("form_field_matrix_insert").submit();
        }
    </script>
<?php
    $form = '<h4>Αντιστοίχηση πεδίων</h4>';
    $form = $form . '<form name="form_field_matrix_insert" id="form_field_matrix_insert" method="post">';
    $form = $form . '<label>Γονική κατηρογία</label>';
    $form = $form . '<select name="parent_id" onchange="my_yoz()"/>';
    $form = $form . '<option selected value="">Επιλέξτε</option>';
    $sql_1 = "SELECT field.id,field.name,field.parameters FROM field where parameters = 'RADIOBOX' OR parameters = 'CHECKBOX' OR parameters = 'DROPDOWNBOX' group by name";
    $result1 = mysqli_query($con, $sql_1);
// echo $sql_1;
    $_POST["click"] = "0";
    // echo $_POST["click"];
    while($row = mysqli_fetch_array($result1))
    {
        if (intval($_POST['parent_id']) == $row['id'])
        {
        $form = $form . '<option selected value="'.$row['id'].'">'.$row['name'].'</option>';
        }
        else{
        $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    }
    $form = $form . '</select>';
    if (!empty($_POST['parent_id'])){
    $form = $form . '<label>Πεδία</label>';
//    $form = $form . '<select name="field_id"/>';
// $form = $form . '<option value="0" selected="selected"</option>';
    $sql_ = "SELECT field.id,field.name FROM field where parameters = 'NULL' group by name";
    $result2 = mysqli_query($con, $sql_);
    $cc=0;
    while($row = mysqli_fetch_array($result2))
    {
        $sql_3 = 'SELECT field_matrix.id as fid, field_matrix.field_id as fmfid,field_matrix.parent_id as fmpi,field_matrix.ordering as fmo FROM field_matrix where field_matrix.field_id='.intval($row['id']).' AND field_matrix.parent_id='.intval($_POST['parent_id']);
        //echo $sql_3.'<br>';
        $result_3 = mysqli_query($con, $sql_3);
        $row3 = mysqli_fetch_array($result_3);
        $cc=$cc+1;
        if (empty($row3))
        {
        $form = $form . '<input type="checkbox" name ="field_id'.$cc.'" value="'.$row['id'].'" /> '.$row['name'].' Ταξινόμηση ';
        $form = $form . '<input type="text" name="ordering'.$cc.'" value="" class="input-mini"/><br>';
        $form = $form . '<input name="id'.$cc.'" type="hidden" value=""/>';
            $form = $form . '<input name="fffid'.$cc.'" type="hidden" value="'.$row['id'].'"/>';
        }
        else
        {
        $form = $form . '<input type="checkbox" name ="field_id'.$cc.'" value="'.$row['id'].'" checked/> '.$row['name'].' Ταξινόμηση ';
        $form = $form . '<input type="text" name="ordering'.$cc.'" value="'.$row3['fmo'].'" class="input-mini"/><br>';
        $form = $form . '<input name="id'.$cc.'" type="hidden" value="'.$row3['fid'].'"/>';
        $form = $form . '<input name="fffid'.$cc.'" type="hidden" value="'.$row['id'].'"/>';
        }
    }
    $form = $form . '<input name="cc" type="hidden" value="'.$cc.'"/>';
    $form = $form . '<input name="click" type="hidden" value="1"/>';
    $form = $form . '<input type="submit" value="Αντιστοίχιση"/>';
    $form = $form . '</form>';

    }
    echo $form;
}
elseif ($_GET['id_ad'] == "1") {
    $sql_2 = "SELECT field.id AS aid, field.name AS aname, field_matrix.id as bid, field_matrix.field_id AS bcid, field_matrix.parent_id AS bparent_id, field_matrix.ordering AS bordering FROM  field JOIN field_matrix WHERE field.id = field_matrix.field_id GROUP BY field_matrix.id";
    $result = mysqli_query($con, $sql_2);
    echo '<table border="1">';
    echo '<tr><td>Id</td><td>Field id</td><td>Name</td><td>Parent_id</td><td>Ordering</td></tr>';
    while($row = mysqli_fetch_array($result))
    {
        echo '<tr><td>'.$row['bid'].'</td><td>'.$row['bcid'].'</td><td>'.$row['aname'].'</td><td>'.$row['bparent_id'].'</td><td>'.$row['bordering'].'</td><td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=2&id='.$row['bid'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=3&id='.$row['bid'].'">Διαγραφή</a> ]</td></tr>';
    }
    echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>

